package com.example.projectdemo.p1;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.projectdemo.p2.AnchorActivity;
import com.example.projectdemo.R;
import com.example.projectdemo.rvrv.RvRvActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/***
 * 创建时间：2020/2/24 17:25
 * 创建人：10850
 * 功能描述：
 */
public class BaseFragment extends Fragment {
    private static String PARAM = "PARAM";
    private String value;
    private int fragmentposition;
    private RecyclerView recyclerView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Bundle arguments = getArguments();
        value = arguments.getString(PARAM);
        fragmentposition = Integer.valueOf(value);
        mRvScrollListener = (RvScrollListener) getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public BaseFragment newInstance(String str) {
        BaseFragment baseFragment = new BaseFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PARAM, str);
        baseFragment.setArguments(bundle);
        return baseFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_base, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView tv = view.findViewById(R.id.tv);
        tv.setText(value);

        recyclerView = view.findViewById(R.id.rv);

        initData();//初始化数据
        mAdapter = new MyAdapter(getActivity(), list);
        //设置适配器
        recyclerView.setAdapter(mAdapter);
        //布局有3种 LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager
        mLayout = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayout);
        //设置Item增加、移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加默认分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(
                getActivity(), DividerItemDecoration.VERTICAL));
        //点击和长按事件
        mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getActivity(), ((TextView) view).getText() + " click", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), AnchorActivity.class);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Intent intent = new Intent(getActivity(), RvRvActivity.class);
                startActivity(intent);

                Toast.makeText(getActivity(), position + " Long click", Toast.LENGTH_SHORT).show();
                mAdapter.removeData(position);
                mAdapter.notifyDataSetChanged();
            }
        });

        //recyclerView.setOnScrollListener(mOnScrollListener);
        recyclerView.addOnScrollListener(mOnScrollListener);
    }


    private MyAdapter mAdapter;//适配器
    private List<String> list;//数据集合
    private LinearLayoutManager mLayout;//布局管理器

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i <= 50; i++) {
            list.add("Item " + i);
        }
    }

    private boolean isUp;


    private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
//            if (fragmentposition != 0) {
//                //如果不是第一个fragment则返回
//                return;
//            }


            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            //得到当前列表第一个完全显示的item的position
            int position = layoutManager.findFirstCompletelyVisibleItemPosition();
            if (position == 0) {
                //如果position为0表示列表正处于顶部
                mRvScrollListener.inTop(true, recyclerView);
            } else {
                mRvScrollListener.inTop(false, recyclerView);
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            Log.i("zxd", "onScrolled: =============");
            //判断滑动方向，recycleView item 上拉 下滑不同动画
            if (dy > 0) {
                isUp = true;
                mAdapter.setStatus(true);
            } else {
                isUp = false;
                mAdapter.setStatus(false);
            }

//            if (fragmentposition != 0) {
//                return;
//                //如果不是第一个fragment则返回
//            }

            //过滤掉一些缓慢的滑动
            if (Math.abs(dy) > 10) {
                //滑动方向
                mRvScrollListener.scrollType(dy > 0);
            }
        }
    };

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    private RvScrollListener mRvScrollListener;

    public interface RvScrollListener {
        //滑动方向监听
        void scrollType(boolean direction);

        //是否滑动到顶部监听
        void inTop(boolean top, RecyclerView recyclerView);
    }
}
